package servlet;
//改变servlet 5个合并 修改了页面的请求 用参数传过来 user?action=
import pojo.User;
import service.UserService;
import service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = {"/user"})
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //创建业务层对象  多态
        UserService userservice = new UserServiceImpl();
        req.setCharacterEncoding("utf-8");
        //获取参数 web.xml 里写的
        String action = req.getParameter("action");
        //list 代表全查询
        if("list".equals(action)){
            //接收参数
            String name = req.getParameter("name");//和页面的name属性保持一致
            String pass = req.getParameter("pass");
            String currentPage = req.getParameter("currentPage");
            if(currentPage==null){
                currentPage="1";
            }
            int intcurrentPage=Integer.parseInt(currentPage);
            User condition = new User(name, pass);

            List<User> users = userservice.findAll(condition, intcurrentPage);

            req.setAttribute("users",users);
            req.setAttribute("condition",condition);
            //转发
            req.getRequestDispatcher("/list.jsp").forward(req,resp);
        }else if("add".equals(action)){
            //拿到页面传递的参数
            String name = req.getParameter("name");//和页面的name属性保持一致
            String pass = req.getParameter("pass");
            //定义业务层对象

            int result = userservice.addUser(new User(name, pass));
            if (result > 0) {
                //重定向 再次查询(和浏览器的输入/user效果一样)          之前写的是转发
                //跳转到user
                resp.sendRedirect("user?action=list");
            }
        }else if("delete".equals(action)){
            //拿到页面传递的参数  拿到的数据都是String类型
            String sid = req.getParameter("id");//和页面的id属性保持一致

            Integer id=Integer.parseInt(sid);

            //定义业务层对象


            //根据id查询用户信息
            int result =userservice.deleteUserById(id);

            if(result>0){
                //重定向 再次查询(和浏览器的输入/user效果一样)
                resp.sendRedirect("user?action=list");
            }
        }else if("toupdate".equals(action)){
            //拿到页面传递的参数  拿到的数据都是String类型
            String sid = req.getParameter("id");//和页面的id属性保持一致

            Integer id=Integer.parseInt(sid);

            //定义业务层对象

            //根据id查询用户信息
            User user=userservice.findById(id);

            //存储对象到作用域里
            req.setAttribute("user",user);

            //转发
            req.getRequestDispatcher("update.jsp").forward(req,resp);

        }else if("update".equals(action)){
            //拿到页面传递的参数
            //获取页面隐藏id
            String sid = req.getParameter("id");
            Integer id=Integer.parseInt(sid);
            String name = req.getParameter("name");//和页面的name属性保持一致
            String pass = req.getParameter("pass");
            //定义业务层对象UserService userService = new UserServiceImpl();

            int result = userservice.updateUser(new User(id,name, pass));
            if (result > 0) {
                //重定向 再次查询(和浏览器的输入/user效果一样)          之前写的是转发
                //跳转到user
                resp.sendRedirect("user?action=list");
            }
        }

    }
}
